package com.klaus.ohMySpace.controller;

import com.alibaba.fastjson.JSONObject;
import com.klaus.ohMySpace.conf.Constants;
import com.klaus.ohMySpace.entity.User;
import com.klaus.ohMySpace.service.TokenService;
import com.klaus.ohMySpace.service.UserService;
import com.klaus.ohMySpace.sys.common.ErrorCode;
import com.klaus.ohMySpace.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static com.klaus.ohMySpace.util.Params.*;

/**
 * Created by Mklaus on 15/8/16.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private TokenService tokenService;

    @RequestMapping(value = "login")
    public String login(){
        return "/user/login";
    }

    @RequestMapping("doLogin")
    public String doLogin(String username,String password, Model model,HttpSession session){
        User login;
        if ((login = userService.login(new User(username,password))) != null){
            session.setAttribute(Constants.USER_UID,login.getId());
            model.addAttribute("user",login);
            return "/user/space";
        }
        return "error";
    }


    @RequestMapping("logout")
    public String logout(HttpServletRequest req){
        req.getSession().removeAttribute(Constants.USER_UID);
        return "redirect:/index.jsp";
    }

    @RequestMapping(value = "register")
    public String register(){
        return "/user/register";
    }

    @RequestMapping(value = "doRegister" ,method = RequestMethod.POST)
    public String register(String username,String password, Model model,HttpSession session){
        User u = new User(username,password);
        userService.add(u);
        model.addAttribute("message","注册成功,请登录");
        return "/user/login";
    }

    //-------------------------- for ios app --------------------------
    @RequestMapping(value = "app_login")
    public void app_login(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String email    = needString(req,"email");
        String password = needString(req,"password");
        String deviceId = needString(req,"deviceId");

        User login;
        String token;

        if ((login = userService.login(new User(email,password))) != null) {
            token = tokenService.saveOrUpdate(login.getId(),deviceId);
            JSONObject json = new JSONObject();
            json.put("token",token);
            JsonUtil.write(login, "user", json, resp);
        }else {
            JsonUtil.writeError(ErrorCode.userNotExist,resp);
        }
    }

    @RequestMapping(value = "app_register")
    public void app_register(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String email    = needString(req,"email");
        String password = needString(req,"password");
        String deviceId = needString(req,"deviceId");

        User login;
        String token;

        if (!userService.isExist(new User(email,password))) {
            login = userService.register(new User(email,password));
            token = tokenService.saveOrUpdate(login.getId(),deviceId);
            JSONObject json = new JSONObject();
            json.put("token",token);
            JsonUtil.write(login, "user", json, resp);
        }else {
            JsonUtil.writeError(ErrorCode.emailBeTake,resp);
        }
    }

    @RequestMapping("app_logout")
    public void appLogout(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        String token   = getString(req,"token");
        Integer userId = getInt(req,"userId");
        if (token == null && userId == null){
            JsonUtil.writeError(ErrorCode.missParameter,"token | userId",resp);
        }else {
            JsonUtil.write(null,null,resp);
        }
    }
}
